//
//  PainterUserKLineData.m
//  p4
//
//  Created by CaoJun on 15/5/21.
//  Copyright (c) 2015年 com.baidao. All rights reserved.
//

#import "PainterUserKLineData.h"

using namespace std;
using namespace ytx;

@implementation PainterUserKLineData

- (void) drawLine:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ytx::ViewModelIndexPosition *) indexPosition withUserGraph:(ytx::UserGraph) userGraph withDrawSubline:(BOOL)isDrawSubline
{
    KLineData * data = (KLineData *)indexPosition->getData();
    Rectangle rect = indexPosition->getRectangle();
    
    int index1 = data->getKLineItemIndexByDateTime(userGraph.p1.dataX);
    int index2 = data->getKLineItemIndexByDateTime(userGraph.p2.dataX);
    
    if (userGraph.p1.dataX.is_not_a_date_time() && !userGraph.p1.preDataX.is_not_a_date_time()) {
        index1 = data->getKLineItemIndexByDateTime(userGraph.p1.preDataX)+1;
    }
    
    if (userGraph.p2.dataX.is_not_a_date_time() && !userGraph.p2.preDataX.is_not_a_date_time()) {
        index2 = data->getKLineItemIndexByDateTime(userGraph.p2.preDataX)+1;
    }
    
    if (index1 < 0 || index2 < 0) {
        return;
    }
    
    float zoomX2 = rect.getZoomX() / 2;
    
    CGFloat x1 = indexPosition->getViewPxByDataIndexWithoutModify(index1), y1 = rect.getPxY(userGraph.p1.dataY), x2 = indexPosition->getViewPxByDataIndexWithoutModify(index2), y2 = rect.getPxY(userGraph.p2.dataY);

    
    [ctx drawLineWithLineWidth:userGraph.lineWidth WidthLineColor:[NSString stringWithUTF8String:userGraph.lineColor.c_str()] withX1:x1+zoomX2 withY1:y1 withX2:x2+zoomX2 withY2:y2];
    [ctx clipToRect:CGRectMake(rect.getBeginX(), rect.getBeginY(), rect.getRectWidth(), rect.getRectHeight())];
    
    if (isDrawSubline) {
        [self drawSubline:cgrect withContext:ctx withViewModelIndexPosition:indexPosition withPoint1:CGPointMake(x1+zoomX2, y1) withDataIndex1:index1 withPoint2:CGPointMake(x2+zoomX2, y2) withDataIndex2:index2];
    }
}

- (void) drawRectangle:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ytx::ViewModelIndexPosition *) indexPosition withUserGraph:(ytx::UserGraph) userGraph withDrawSubline:(BOOL)isDrawSubline
{
    KLineData * data = (KLineData *)indexPosition->getData();
    Rectangle rect = indexPosition->getRectangle();
    
    int index1 = data->getKLineItemIndexByDateTime(userGraph.p1.dataX);
    int index2 = data->getKLineItemIndexByDateTime(userGraph.p2.dataX);
    
    if (userGraph.p1.dataX.is_not_a_date_time() && !userGraph.p1.preDataX.is_not_a_date_time()) {
        index1 = data->getKLineItemIndexByDateTime(userGraph.p1.preDataX)+1;
    }
    
    if (userGraph.p2.dataX.is_not_a_date_time() && !userGraph.p2.preDataX.is_not_a_date_time()) {
        index2 = data->getKLineItemIndexByDateTime(userGraph.p2.preDataX)+1;
    }
    
    if (index1 < 0 || index2 < 0) {
        return;
    }
    
    float zoomX2 = rect.getZoomX() / 2;
    
    CGFloat x1 = indexPosition->getViewPxByDataIndexWithoutModify(index1), y1 = rect.getPxY(userGraph.p1.dataY), x2 = indexPosition->getViewPxByDataIndexWithoutModify(index2), y2 = rect.getPxY(userGraph.p2.dataY);
    
    if (isDrawSubline) {
       [self drawSubline:cgrect withContext:ctx withViewModelIndexPosition:indexPosition withPoint1:CGPointMake(x1+zoomX2, y1) withDataIndex1:index1 withPoint2:CGPointMake(x2+zoomX2, y2) withDataIndex2:index2];
    }
    
    [ctx drawRectWithX1:x1 + zoomX2 withY1:y1 withX2:x2 + zoomX2 withY2:y2 withLineWidth:userGraph.lineWidth withLineColor:[NSString stringWithUTF8String:userGraph.lineColor.c_str()]];
    [ctx clipToRect:CGRectMake(rect.getBeginX(), rect.getBeginY(), rect.getRectWidth(), rect.getRectHeight())];
}

- (void) drawSubline:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ytx::ViewModelIndexPosition *) indexPosition withPoint1:(CGPoint) point1 withDataIndex1:(int) dataIndexX1 withPoint2:(CGPoint) point2 withDataIndex2:(int) dataIndexX2
{
    KLineData * data = (KLineData *)indexPosition->getData();
    [self drawCrosshairsX:cgrect withContext:ctx withViewModelIndexPosition:indexPosition withPx:point2.x];
    [self drawCrosshairsY:cgrect withContext:ctx withViewModelIndexPosition:indexPosition withPx:point2.y];
    NSString * textY2 = [NSString stringWithUTF8String:Tools::Convert2FixString( indexPosition->getRectangle().getValueY(point2.y), indexPosition->getData()->getToFix() ).c_str()];
    [self drawCrosshairsYText:cgrect withContext:ctx withViewModelIndexPosition:indexPosition withPx:point2.y withText:textY2];
    
    string textString2 = LineType::isMinuteLine(data->quotationType) ? data->kLineData[dataIndexX2].dateTime.toStringYYYYMMddHHmmss() : data->kLineData[dataIndexX2].dateTime.toStringYYYYMMdd();
    
    NSString * textX2 = [NSString stringWithUTF8String: textString2.c_str() ];
    [self drawCrosshairsXText:cgrect withContext:ctx withViewModelIndexPosition:indexPosition withPx:point2.x withText:textX2];
    
    [self drawCrosshairsX:cgrect withContext:ctx withViewModelIndexPosition:indexPosition withPx:point1.x];
    [self drawCrosshairsY:cgrect withContext:ctx withViewModelIndexPosition:indexPosition withPx:point1.y];
    
    
    NSString * textY1 = [NSString stringWithUTF8String:Tools::Convert2FixString( indexPosition->getRectangle().getValueY(point1.y), indexPosition->getData()->getToFix() ).c_str()];
    [self drawCrosshairsYText:cgrect withContext:ctx withViewModelIndexPosition:indexPosition withPx:point1.y withText:textY1];
    
    string textString1 = LineType::isMinuteLine(data->quotationType) ? data->kLineData[dataIndexX1].dateTime.toStringYYYYMMddHHmmss() : data->kLineData[dataIndexX1].dateTime.toStringYYYYMMdd();
    
    NSString * textX1 = [NSString stringWithUTF8String: textString1.c_str() ];
    [self drawCrosshairsXText:cgrect withContext:ctx withViewModelIndexPosition:indexPosition withPx:point1.x withText:textX1];
}

@end
